/**
 * 
 */
package org.puresilk.android.net.services;

import org.puresilk.android.net.HttpException;
import org.puresilk.android.net.HttpRequest;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;

/**
 * Bound service to submit transactions to server
 * applications.  The service will attempt to post
 * the action request to the server (via the url).
 * If there is a network problem (like no network)
 * then the service will startup the ActionQueueService
 * and pass on the requests for eventual resolution.  
 * 
 * @author david
 *
 */
public class ActionSubmissionService extends Service implements ActionService {
	
	// submit request results
	public static final int ACTION_SENT = 0;
	public static final int ACTION_QUEUED = 1;
	public static final int ACTION_ERROR = -1;
	
	public static final int QUEUEING = 1;
	public static final int NOT_QUEUEING = 0;
	public static final int FLUSHING_QUEUE = 2;
	
	// attributes
	private int rspProto = HttpRequest.JSON_REQUEST_TYPE;
	private int _queueActions = NOT_QUEUEING;
	
	/** default constructor */
	public ActionSubmissionService() { }

	/** protocol specified constructor */
	public ActionSubmissionService(int proto){
		rspProto = proto;
	}
	
	/**
     * Inner Binder class used for to allow clients to bind to service.  Because we know this 
     * service always runs in the same process as its clients, we don't need to deal with IPC.
     */
    protected Binder assBinder = new Binder() {
    	/**
    	 * API to submit action request.
    	 * 
    	 * @param url
    	 * @param data
    	 */
        @SuppressWarnings("unused")
		public void submit(String url, String data) {
    		ActionSubmissionService.this.submitAction(url, data);
        }
    };
    
    /* (non-Javadoc)
	 * @see android.app.Service#onBind(android.content.Intent)
	 */
	@Override
	public IBinder onBind(Intent arg0) {
		return assBinder;
	}

	/**
	 * Submit HttpRequest for server actions.  Prepares
	 * an instance of ReqHandler by setting run parameters
	 * so we can report on them on the other side.  Also
	 * we are using the service's default protocol which
	 * defaults to expect JSON return messages.
	 * 
	 * @param action
	 */
	public void submitAction(String url, String data){
		try {
			// send to queue if no network status...
			switch (_queueActions ) {
				case QUEUEING: 
					// offline, bail 
					throw new HttpException("Sending to Queue.");
					
				case FLUSHING_QUEUE:
					// wait for it...
					while (_queueActions == FLUSHING_QUEUE)
						try { Thread.sleep(1000); } catch(Exception ex) {}
						
				case NOT_QUEUEING:		
					// need a specific instance for each url
					ReqHandler rh = new ReqHandler(this);
					rh.setData(data);
					rh.setUrl(url);
					
					// request from Server
					HttpRequest rpc = new HttpRequest(url, data, rspProto);
					rpc.fetch((Context)this, rh);
					}
		}
		catch (HttpException ex) {
			// network error, save to action queue	
			queueTransaction(url, data);

			// start queue service if not runnint
			if (_queueActions != QUEUEING) {
				// set flag to start queuing
				_queueActions = QUEUEING;
				
				// start up queue service...
				ActionQueueService aqs = new ActionQueueService();
				aqs.startService(null);
			}
		}
	}
	
	/**
	 * Alert UI w/ TOAST message.
	 * @param resp
	 */
	public void notifications(String msg) {
		notifications(msg,ActionService.TOAST_ALERT);
	}
	
	/**
	 * Multi-type Alert Message.
	 * @param msg
	 * @param type
	 */
	public void notifications(String msg, int type){
		if(type == ActionService.TOAST_ALERT) {
			
		}
		else {
			// type == STATUS_Alert
		}
			
	}

	/**
	 * Queue transaction for later by
	 * sticking it into the action queue 
	 * table in on-board db.
	 * 
	 * @param url
	 * @param data
	 */
	private void queueTransaction(String url, String data){
		
		
	}
		
}
